from model.admin_model import Admin
from database_connection.index import Session
from utils.utils import add_model
session = Session()


def add_one_admin(user_info):
    try:
        admin = add_model(user_info, "admin")
        session.add(admin)
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        session.commit()
        session.flush()
        session.refresh(admin)
        return {"msg": "success", "data": admin}


def find_one_admin(user_info):
    # user_info需要查询的条件
    try:
        data = session.query(Admin).filter_by(**user_info).first()
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
    else:
        return {"msg": "success", "data": data}
